<?php
/**
*作用:
*说明:
*版权:
*作者:Red	QQ:316765128
*时间:2011/7/28
**/
include_once './include/init.inc.php';

//red add
$c = (empty($_GET['c']) or !is_numeric($_GET['c']))?'all':$_GET['c'];
$g = (empty($_GET['g']) or !is_numeric($_GET['g']))?'all':$_GET['g'];
$t = (empty($_GET['t']) or !is_numeric($_GET['t']))?'all':$_GET['t'];
$m = (empty($_GET['m']) or !is_numeric($_GET['m']))?'all':$_GET['m'];
$smarty->assign('c',$c);
$smarty->assign('g',$g);
$smarty->assign('t',$t);
$smarty->assign('m',$m);

//搜索
$arr = $g_arr = $t_arr = $m_arr = array();
$gg = $tt = $mm = 0;
if($g != 'all'){
	$sql = "select goods_id from `info_goods_cat` where cat_id=".$g;
	$g_all = $db->fetch_all($sql);
	foreach($g_all as $key=>$val){
		$arr[] = $val['goods_id'];
	}
	$gg = 1;
}
if($t != 'all'){
	$sql = "select goods_id from `info_goods_cat` where cat_id=".$t;
	$t_all = $db->fetch_all($sql);
	foreach($t_all as $key=>$val){
		$arr2[] = $val['goods_id'];
	}
	$tt = 1;
	if($gg == 1){
		$arr = array_intersect($arr,$arr2);
	}else{
		$arr = $arr2;
	}
}
if($m != 'all'){
	$sql = "select goods_id from `info_goods_cat` where cat_id=".$m;
	$m_all = $db->fetch_all($sql);
	foreach($m_all as $key=>$val){
		$arr3[] = $val['goods_id'];
	}
	$mm = 1;
	if($tt == 1 or $gg == 1){
		$arr = array_intersect($arr,$arr3);
	}else{
		$arr = $arr3;
	}
}


$sql = "select `goods_name`,`goods_id`,`original_img` from `info_goods` WHERE ";
if($c=='all'){
	$sql .= "cat_id in (2,64,65,66,3)";
}else{
	$sql .= 'cat_id='.$c;
}
//Q+特殊功能，统计各个属性下的商品数量，最后用到。
$dog_all_new = $db->fetch_all($sql);
$in_goods_id_array = '';
if(!empty($dog_all_new)){
	$in_goods_id_array = 'and gc.goods_id in (0';
	foreach($dog_all_new as $key=>$val){
		$in_goods_id_array .= ','.$val['goods_id'];
	}
	$in_goods_id_array .= ')';
}
//end
$dog_all = $db->fetch_all($sql);
if($gg or $tt or $mm){
	if(!empty($arr)){
		$s = implode(',',$arr);
		$sql .= " and goods_id in (".$s.")";
	}else{
		$sql .= " and 0 ";
	}
}

$dog_all = $db->fetch_all($sql);
$dogs = array();
$i=0;
foreach($dog_all as $key=>$val){
	$attr = array();
	$attr = get_goods_properties($val['goods_id'],$db); //属性
	$val['attr'] = @array_reverse($attr['pro']['商品属性']);
	$val['attr'] = @array_slice($val['attr'], 0, 4);
	if($i==3){
		$i = 0;
	}
	$val['num'] = $i;
	$dogs[] = $val;
	++$i;
}
$smarty->assign('dogs',       $dogs);
$smarty->assign('dog_num',       count($dog_all));

$sortname = array('all'=>'所有宠物','64'=>'大型犬 (63)','65'=>'中型犬 (39)','66'=>'小型犬 (47)','3'=>'猫咪 (40)');
$smarty->assign('sortname',       $sortname[$c]);


//功能
$sql = "select c.cat_id,c.cat_name,count(gc.cat_id) as num from `info_category` as c left outer join `info_goods_cat` as gc on c.cat_id=gc.cat_id where c.parent_id=61 ".$in_goods_id_array." group by c.cat_id order by c.sort_order desc";
$red_g = $db->fetch_all($sql);
$smarty->assign('red_g',       $red_g);

//特性
$sql = "select c.cat_id,c.cat_name,count(gc.cat_id) as num from `info_category` as c left outer join `info_goods_cat` as gc on c.cat_id=gc.cat_id where c.parent_id=60 ".$in_goods_id_array." group by c.cat_id order by c.sort_order desc";
$red_t = $db->fetch_all($sql);
$smarty->assign('red_t',       $red_t);

//毛色
$sql = "select c.cat_id,c.cat_name,count(gc.cat_id) as num from `info_category` as c left outer join `info_goods_cat` as gc on c.cat_id=gc.cat_id where c.parent_id=59 ".$in_goods_id_array." group by c.cat_id order by c.sort_order desc";
$red_m = $db->fetch_all($sql);
$smarty->assign('red_m',       $red_m);
//red end

$smarty->assign('nav','daquan');
fheader();
$smarty->display('list.dwt');
ffooter();